/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_A4_phi_vs_z_byyear.do
Date: October 2022

Description: Reproduces Table A.4: Contribution of aggregate firm-embedded productivity, by year.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/table_A4_phi_vs_z_byyear.log",replace
*-------------------------------------------------------------------------------


*Creating a placeholder dataset
*----------------------------------------------
clear all
set obs 100
gen year=.
local iter=0
forvalues year=2006(1)2017 {
display "`year'"
local iter=`iter'+1
replace year=`year' if _n==`iter'
}
gen coef=.
gen se=.
gen num_ctry=.
gen coef_cttesample=.
gen se_cttesample=.
gen num_ctry_cttesample=.

tempfile phi_byyear
save `phi_byyear', replace 



*-----------------------------------------------------------------
*Table A4 (part a): coef and standard error per year, for all firms
*-----------------------------------------------------------------
local nn=10 
*minimum number of sectors allowed per year-isocode*/
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
tempfile file_byyear
save `file_byyear', replace 


*-----------------------------------------------------------------
clear all
use `file_byyear', clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
keep if year>=2006 & year<=2017
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

tempfile temp
save `temp', replace 



forvalues year=2006(1)2017 {
display "`year'"

use `temp', clear
reg `LHS1' `RHS' if year==`year'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
	
use  `phi_byyear', clear
replace coef=$c1 if year==`year'
replace se=$se1 if year==`year'
replace num_ctry=`e(N)' if year==`year'
save, replace

}
drop if year==.

use  `phi_byyear', clear



*-----------------------------------------------------------------
*Table A4 (part b): coef and standard error per year, for firms that appear every year during the period of analysis
*-----------------------------------------------------------------
local nn=10 
*minimum number of sectors allowed per year-isocode*/
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent_ctte.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${data}/estimates_agg_naics_sales_s1_base_woparent_ctte.dta", clear
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
tempfile file_byyear
save `file_byyear', replace 


*-----------------------------------------------------------------
clear all
use `file_byyear', clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
keep if year>=2006 & year<=2017
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

tempfile temp
save `temp', replace 



forvalues year=2007(1)2017 {
display "`year'"

use `temp', clear
reg `LHS1' `RHS' if year==`year'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$c1, $se1"
use  `phi_byyear', clear
replace coef_cttesample=$c1 if year==`year'
replace se_cttesample=$se1 if year==`year'
replace num_ctry_cttesample=`e(N)' if year==`year'
save, replace

}

use  `phi_byyear', clear
drop if year==.

*Export table to excel
export delimited using "${rappendix}/table_A4_phi_vs_z_byyear.csv", datafmt replace

log close




	